package client.gui;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import client.logic.Facade;
import client.logic.chat.Chatter;
import encription.SecurityHandler;


/**
 * chat frame to send and view chat messages 
 */
public class frmChatFrame extends JFrame {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JLabel lblChat;
	private JLabel lblMessage;
	private JScrollPane jScrollPane1;
	private JPanel jPanel1;
	private JTextArea txtChatWindow;
	private JTextField txtChat;
	private Chatter _chatter;

	/**
	 * Creates an instance of frmChatFrame
	 * @param c the object which sends the chat messages from this window to the other client
	 * @param name the name of the person to chat with
	 */
	public frmChatFrame(final Chatter c, String name){
		_chatter = c;
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosed(WindowEvent e){
				System.out.println("Closed chat");
				c.close();
				c.reset();
			}
			@Override
			public void windowClosing(WindowEvent e){
				//dispose();
				System.out.println("Closing chat");
				c.close();
				c.reset();
			}
		});
		initGUI(name);
	}

	/**
	 * Initializes the GUI
	 * @param name the name to view at the top of the screen
	 */
	private void initGUI(String name) {
		try {
			{
				getContentPane().setLayout(null);
				{
					lblChat = new JLabel();
					getContentPane().add(lblChat);
					lblChat.setText("Chat with " + name);
					lblChat.setBounds(113, 6, 152, 23);
				}
				{
					lblMessage = new JLabel();
					getContentPane().add(lblMessage);
					lblMessage.setText("Message");
					lblMessage.setBounds(35, 120, 68, 14);
				}
				{
					txtChat = new JTextField();
					getContentPane().add(txtChat);
					txtChat.setBounds(101, 118, 261, 20);
					txtChat.addKeyListener(new KeyListener(){
						@Override
						public void keyPressed(KeyEvent e) {}

						@Override
						public void keyReleased(KeyEvent e) {
							if (e.getKeyCode() == KeyEvent.VK_ENTER){
								sendMessage(txtChat.getText());
								System.out.println(txtChat.getText());
								txtChat.setText("");
							}
						}

						@Override
						public void keyTyped(KeyEvent e) {}
					});
				}
				{
					jPanel1 = new JPanel();
					BoxLayout jPanel1Layout = new BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS);
					jPanel1.setLayout(jPanel1Layout);
					getContentPane().add(jPanel1);
					jPanel1.setBounds(32, 29, 329, 90);
					{
						jScrollPane1 = new JScrollPane();
						jPanel1.add(jScrollPane1);
						jScrollPane1.setPreferredSize(new java.awt.Dimension(329, 90));
						{
							txtChatWindow = new JTextArea();
							jScrollPane1.setViewportView(txtChatWindow);
							txtChatWindow.setAutoscrolls(true);
							//txtChatWindow.setBounds(32, 29, 329, 90);
							txtChatWindow.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
							txtChatWindow.setEditable(false);
						}
					}
				}
				this.setSize(399, 191);
			}
		} catch(Exception e) {
			SecurityHandler.catchException("initializing chat frame", e, false);
			//e.printStackTrace();
		}

	}
	
	/**
	 * prints the received message into the screen (add it to the chat log)
	 * @param message the message to view
	 */
	public void recieveMessage(String message){
		if (txtChatWindow!=null){
			txtChatWindow.setText(txtChatWindow.getText()+ message +"\n");
			//txtChatWindow.repaint();
		}
	}
	/**
	 * prints a message to the screen and sends it to the foreign client
	 * @param message the message to send and print
	 */
	private void sendMessage(String message){
		if (txtChatWindow!=null){
			txtChatWindow.setText(txtChatWindow.getText()+Facade.getInstance().getLoggedUser()+ "> "+message+"\n");
			//txtChatWindow.repaint();
			Facade.getInstance().sendChatMessage(_chatter, message);
		}
	}

}
